//
//  BKNavigationController.swift
//  BaoKeSwift
//
//  Created by geek-duan on 2017/9/22.
//  Copyright © 2017年 geek-duan. All rights reserved.
//

import UIKit

class NavigationController: UINavigationController, UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.interactivePopGestureRecognizer?.delegate = self
        
        SetUI()
    }

    // MARK: - 重写push 方法来隐藏tabBar 和自定义返回按钮
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        if childViewControllers.count > 0 {
        
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "", fontSize: 16, target: self, action: "popToPreviousVC", isBack: true)
            viewController.hidesBottomBarWhenPushed = true
        }
        super.pushViewController(viewController, animated: true)
    }
    
    // MARK: - 手势识别器对象会调用这个代理方法来决定手势是否有效
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        //当子控制器的个数大于1有效
        return childViewControllers.count > 1
    }
    
    // MARK: - 返回上一个控制器
    @objc private func popToPreviousVC() {
        popViewController(animated: true)
    }
}

// MARK: - 设置UI
extension NavigationController {
    
    private func SetUI() {
        navigationBar.barTintColor = UIColor.themeColor
        navigationBar.isTranslucent = false
        // - 设置导航栏字体和颜色
        navigationBar.titleTextAttributes = [.font: UIFont.systemFont(ofSize: 18), .foregroundColor: UIColor.white]
        /// - 设置黑线
//        navigationBar.setBackgroundImage(UIImage().imageFromColor(color: UIColor.themeColor), for: .default)
        navigationBar.shadowImage = UIImage()
    }
}
